package com.hhxy.web;

import com.alibaba.fastjson.JSON;
import com.hhxy.pojo.Brand;
import com.hhxy.service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/selectAllServlet")
public class SelectAllServlet extends HttpServlet {
    private BrandService service =  new BrandService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取Service对象
        //2、调用Service方法，获取数据库中的数据
        List<Brand> brands = service.selectAll();
        //3、将对象转换成JSON字符串 ， 方便传输到前端页面  （序列化）
        String jsonString = JSON.toJSONString(brands);
//        System.out.println(jsonString);//打印看一下，JSON字符串是数组形式的
        //4、响应数据，将JSON字符串传输到前端页面去
        //4.1 处理中文数据，防止乱码
//        response.setContentType("test/html;charset=utf-8");//这样子改的
        response.setContentType("text/json;charset=utf-8");
        //4.2 响应
        response.getWriter().write(jsonString);
    }
}